<?php

namespace app\common\service\coupon\sync_channel;

use  api\channel\coupon\YiQiDaHandler;
use app\model\CouponProductChannel;
use think\facade\Log;

/**
 * 鲸充Api
 * @document https://www.showdoc.com.cn/yiqida/7223557808701827
 */
class YiQiDaChannel extends Base
{
    const CHANNEL_NAME = 'YiQiDa';

    /**
     * @var YiQiDaHandler $yiqida 亿奇达具体请求接口
     */
    public $yiqida;

    /**
     * @var object $supplier 亿奇达渠道信息
     */
    public $supplier;

    /**
     *初始化构造函数
     */
    public function __construct()
    {
        $this->yiqida = new YiQiDaHandler();
        $this->supplier = $this->yiqida->getSupplier();
    }

    /**
     * 分类处理
     * [[origin_id:1,name:'热门分类']]
     * eg:热门影视
     * note:测试通过
     * 暂不使用
     */
    public function getCate(): array
    {
//        $res = $this->yiqida->getCatalogList();
//        if ($res['code'] != 200) {
//            return web_request([],false, 0, '请稍后再同步');
//        }
//        $cateList = $res['data']['cateList'];
//        Log::info('亿奇达获取分类'.json_encode($cateList));
//        if (empty($cateList)){
//            return web_request([],false, 0, '请稍后再同步');
//        }
//        $result = [];
//        foreach ($cateList as $category) {
//            $temp = [];
//            $temp['origin_id'] = $category['Id'];//可为空
//            $temp['name'] = $category['name'];
//            $result[] = $temp;
//        }
//        $this->cates = $result;
//        return $result;
        return [];
    }

    /**
     *品牌
     * [[origin_id:1,name:'爱奇艺会员']]
     * eg 爱奇艺
     */
    public function getBrand()
    {

    }

    /**
     * 类型 黄金会员
     * @return mixed
     * eg 黄金会员
     */
    public function getType()
    {

    }


    /**
     * 具体产品（第一次拉取渠道商的时候处理）
     * eg 月卡 30天卡
     * 陌陌 普通 会员 1个月
     */
    public function getProduct()
    {

    }

    /**
     * 渠道产品（对应的渠道商产品）
     * 具体渠道产品
     */
    public function getChannelProduct()
    {
//        if (empty($this->cates)) {
//            return web_request([],false, 0, '请稍后再同步');
//        }
//        foreach ($this->cates as $cate) {
            for ($i = 1; $i < 100; $i++) {
                $res = $this->yiqida->getCommodityList($i, $cate??0);
                if ($res['code'] == 200 && !empty($productChannels = $res['data']['commodityList'])) {
                    foreach ($productChannels as $productChannel) {
                        //只对接官方直充的，卡密类的不对接
                        if ($productChannel['virtualDelivery'] != YiQiDaHandler::VIRTUALDELIVERY_ZC) {
                            continue;
                        }
                        //商品状态
                        if ($productChannel['status'] == YiQiDaHandler::PRODUCT_STATUS_ACTIVE) {
                            $status = CouponProductChannel::STATUS_ACTIVE;
                        } else {
                            $status = CouponProductChannel::STATUS_CLOSE;
                        }
                        $productChannel['status'] = $status;
                        $productChannel['image'] = $productChannel['MainImg'];
                        $productChannel['type'] = self::CHANNEL_NAME;
                        $productChannel['supplier_id'] = $this->supplier->id;
                        $productChannel['origin_id'] = $productChannel['mainId'];
                        //$productChannel['sale_price'] = yuan2fen($productChannel['price']) ?? 0;
                        $productChannel['cost_price'] = yuan2fen($productChannel['price']) ?? 0;
                        $productChannel['market_price'] = yuan2fen($productChannel['guidePrice']) ?? 0;
                        $productChannel['sku_type'] = CouponProductChannel::SKU_TYPE_ZC;
                        $productChannel['extend'] = json_encode($productChannel, JSON_UNESCAPED_UNICODE);
                        $this->productChannels[] = $productChannel;
                    }
                }
                if ($i*60 >= $res['data']['total'])  {
                    break;
                }
            }
//        }
            return $this->productChannels;
    }









}